/**
 * Notes: 预约后台管理
 * Ver : CCMiniCloud Framework 2.0.1 ALL RIGHTS RESERVED BY cclinux@qq.com
 * Date: 2021-07-11 07:48:00 
 */

const BaseAdminService = require('./base_admin_service.js');
const MeetModel = require('../../model/meet_model.js');
const JoinModel = require('../../model/join_model.js');
const timeUtil = require('../../../framework/utils/time_util.js');
const util = require('../../../framework/utils/util.js');

class AdminMeetService extends BaseAdminService {

    /** 预约列表 */
    async getMeetList({
        search,
        sortType,
        sortVal,
        orderBy,
        whereEx,
        page,
        size,
        isTotal = true,
        oldTotal
    }) {

        orderBy = orderBy || {
            'MEET_ORDER': 'asc',
            'MEET_ADD_TIME': 'desc'
        };
        let fields = '*';

        let where = {};
        where.MEET_STATUS = 1;

        if (util.isDefined(search) && search) {
            where.MEET_TITLE = {
                $regex: '.*' + search,
                $options: 'i'
            };
        } else if (sortType && util.isDefined(sortVal)) {
            switch (sortType) {
                case 'status':
                    where.MEET_STATUS = Number(sortVal);
                    break;
                case 'sort':
                    if (sortVal == 'view') {
                        orderBy = {
                            'MEET_VIEW_CNT': 'desc',
                            'MEET_ADD_TIME': 'desc'
                        };
                    }
                    if (sortVal == 'new') {
                        orderBy = {
                            'MEET_ADD_TIME': 'desc'
                        };
                    }
                    break;
            }
        }

        return await MeetModel.getList(where, fields, orderBy, page, size, isTotal, oldTotal);
    }

    /** 删除预约 */
    async delMeet(id) {
        await MeetModel.del({
            _id: id
        });
    }

    /** 更新预约 */
    async editMeet({
        id,
        title,
        order,
        typeId,
        typeName,
        content,
        forms
    }) {

        let data = {
            MEET_TITLE: title,
            MEET_ORDER: order,
            MEET_TYPE_ID: typeId,
            MEET_TYPE_NAME: typeName,
            MEET_CONTENT: content,
            MEET_FORMS: forms
        }
        await MeetModel.edit({
            _id: id
        }, data);
    }

    /** 添加预约 */
    async insertMeet({
        title,
        order,
        typeId,
        typeName,
        content,
        forms
    }) {

        let data = {
            MEET_TITLE: title,
            MEET_ORDER: order,
            MEET_TYPE_ID: typeId,
            MEET_TYPE_NAME: typeName,
            MEET_CONTENT: content,
            MEET_FORMS: forms,
            MEET_STATUS: 1,
            MEET_ADD_TIME: timeUtil.time()
        }

        await MeetModel.insert(data);
    }

    /** 预约状态修改 */
    async statusMeet(id, status) {
        let data = {
            MEET_STATUS: status
        }
        await MeetModel.edit({
            _id: id
        }, data);
    }

    /** 报名状态修改 */
    async statusJoin(joinId, status, reason = '') {
        let data = {
            JOIN_STATUS: status,
            JOIN_REASON: reason
        }
        await JoinModel.edit({
            _id: joinId
        }, data);
    }

    /** 预约数据列表 */
    async getJoinList({
        search,
        sortType,
        sortVal,
        orderBy,
        meetId,
        page,
        size,
        isTotal = true,
        oldTotal
    }) {

        orderBy = orderBy || {
            'JOIN_ADD_TIME': 'desc'
        };
        let fields = '*';

        let where = {
            JOIN_MEET_ID: meetId
        };

        if (util.isDefined(search) && search) {
            where['JOIN_FORMS.val'] = {
                $regex: '.*' + search,
                $options: 'i'
            };
        } else if (sortType && util.isDefined(sortVal)) {
            switch (sortType) {
                case 'status':
                    where.JOIN_STATUS = Number(sortVal);
                    break;
            }
        }

        return await JoinModel.getList(where, fields, orderBy, page, size, isTotal, oldTotal);
    }
}

module.exports = AdminMeetService;